//
//  ZhuCeViewController.m
//  XiangMu
//
//  Created by 康世标 on 15/8/15.
//  Copyright (c) 2015年 康世标. All rights reserved.
//

#import "ZhuCeViewController.h"

@interface ZhuCeViewController ()

@end

@implementation ZhuCeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    _sJtiemr=50;
    [_imagView1.layer setBorderWidth:.6];
    [_imagView2.layer setBorderWidth:.6];
    [_imagView3.layer setBorderWidth:.6];
    [_imagView4.layer setBorderWidth:.6];
    [_yanZhangBtn setBackgroundColor:CL_00cc85];
    
    UIButton*iamgeview=[UIButton buttonWithType:UIButtonTypeCustom];
    iamgeview.frame=CGRectMake(10, 0, 10, 20);
    [iamgeview setImage:[UIImage imageNamed:@"fanhui"] forState:UIControlStateNormal];
    
    [iamgeview addTarget:self action:@selector(btn) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem*bar=[[UIBarButtonItem alloc]initWithCustomView:iamgeview];
    
    self.navigationItem.leftBarButtonItem=bar;
    if (self.pswBool) {
        
        [self.upDatPsw setBackgroundImage:[UIImage imageNamed:@"denglu.png"] forState:UIControlStateNormal];
        
    }
}
#pragma  mark---验证码
-(IBAction)YanZhengma:(UIButton *)sender {
    
    
    BOOL isNo=[self validateMobile:_textField1.text];
    if (!isNo) {
        
        [[[UIAlertView alloc]initWithTitle:@"温馨提示" message:@"请输入正确的手机号码" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"取消", nil]show];
        
        return;
    }
    
    NSString * url=nil;
    if (self.pswBool) {
        
        url=URL_XIUGAIMIMAYANZHENGMA;
        
    }
    else{
     
        url =URL_HUOQUYANZHANGMA;
    }
    
   [QingQiuShuJu HuoQuYanZhengMa:self.textField1.text url:url Hui:^(NSString *zhu, BOOL isOK)
    {
 
        if (isOK)
        {
            
            _timer =[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerSta) userInfo:nil repeats:YES];
            self.yanZhangBtn.userInteractionEnabled=NO;
           
            
        }
        else{
            _hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
            _hud.mode=MBProgressHUDModeText;
            _hud.labelText = zhu;
            _hud.margin = 10.f;
            
            _hud.removeFromSuperViewOnHide = YES;
            
            [_hud hide:YES afterDelay:1];

        }
        
    }];
    
    
    
}
#pragma mark ----注册
-(IBAction)ZhuCe:(UIButton *)sender{
    _hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    _hud.mode=  MBProgressHUDModeText;
    [_hud hide:YES afterDelay:1.5];
    
    _hud.removeFromSuperViewOnHide = YES;
    
       BOOL isNo=[self validateMobile:_textField1.text];
    if (!isNo) {
        
          _hud.labelText = @"请输入正确的手机号码";
        return;
    }

    if (self.textField1==nil||[self.textField1.text isEqualToString:@""]) {
           _hud.labelText = @"手机号码不能为空";
        return;
    }
    
    if (self.textField2==nil||[self.textField2.text isEqualToString:@""]) {
          _hud.labelText = @"请输入验证码";
        return;
    }
    if (self.textField3==nil||[self.textField3.text isEqualToString:@""])
    {
        _hud.labelText = @"请输入密码";
        return;
    }
    if (self.textField3.text.length<6) {
        _hud.labelText = @"密码太短,最少6位";
        return;
    }
    
    if (![self.textField3.text isEqualToString:self.textField4.text]) {
        _hud.labelText = @"两次密码不一致";
        
        return;
    }
    NSString * url =nil;
    
    if (self.pswBool) {
//        修改密码
        url = URL_XIUGAIMIMAJIEKOU;
    }
    else{
//        注册
        
        url = URL_ZHUCE;
    }
    
   _hud.labelText = @"数据加载中";
    

    [QingQiuShuJu ZhuCe:self.textField1.text YanZhengMa:self.textField2.text PsW:self.textField4.text url:url HuiChuan:^(NSString *zhu, BOOL isOK)
     {
        
        if (isOK) {
//            _hud.labelText=zhu;
//            注册成功
            _hud.mode = MBProgressHUDModeCustomView;
            
            _hud.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"37x-Checkmark.png"]];
           
            if (self.pswBool)
            {
                 _hud.labelText = @"修改成功";
                //        修改密码
                
            }
            else
            {
                //        注册
                
                _hud.labelText = @"注册成功";
            }

          
            
            [self performSelector:@selector(back1) withObject:self afterDelay:1.5];
            
            
            
        }
        else{
            _hud.labelText=zhu;
        }
    }];
    
   

}
- (void)back1{
    
    [self.navigationController popViewControllerAnimated:YES];
    
}
-(void)timerSta{
    
    _sJtiemr--;
    
    [self.yanZhangBtn setTitle:[NSString stringWithFormat:@"%d",_sJtiemr] forState:UIControlStateNormal];
    
    if (_sJtiemr==0) {
        
        if ([self.timer isValid]) {
            
            [self.timer invalidate];
        }
        self.sJtiemr=50;
        [self.yanZhangBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
        self.yanZhangBtn.userInteractionEnabled=YES;
    }
    

}
#pragma  mark ---返回按钮
-(void)btn{
    
    [self.navigationController popViewControllerAnimated:YES];
}

- (BOOL)validateMobile:(NSString *)mobileNum
{
    if (mobileNum==nil) {
        return NO;
    }
    /**
     * 手机号码
     * 移动：134[0-8],135,136,137,138,139,150,151,157,158,159,182,187,188
     * 联通：130,131,132,152,155,156,185,186
     * 电信：133,1349,153,180,189
     */
    NSString * MOBILE = @"^1(3[0-9]|5[0-35-9]|8[025-9])\\d{8}$";
    /**
     10         * 中国移动：China Mobile
     11         * 134[0-8],135,136,137,138,139,150,151,157,158,159,182,187,188
     12         */
    NSString * CM = @"^1(34[0-8]|(3[5-9]|5[017-9]|8[278])\\d)\\d{7}$";
    /**
     15         * 中国联通：China Unicom
     16         * 130,131,132,152,155,156,185,186
     17         */
    NSString * CU = @"^1(3[0-2]|5[256]|8[56])\\d{8}$";
    /**
     20         * 中国电信：China Telecom
     21         * 133,1349,153,180,189
     22         */
    NSString * CT = @"^1((33|53|8[09])[0-9]|349)\\d{7}$";
    /**
     25         * 大陆地区固话及小灵通
     26         * 区号：010,020,021,022,023,024,025,027,028,029
     27         * 号码：七位或八位
     28         */
    // NSString * PHS = @"^0(10|2[0-5789]|\\d{3})\\d{7,8}$";
    
    NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", MOBILE];
    NSPredicate *regextestcm = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CM];
    NSPredicate *regextestcu = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CU];
    NSPredicate *regextestct = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CT];
    
    if (([regextestmobile evaluateWithObject:mobileNum] == YES)
        || ([regextestcm evaluateWithObject:mobileNum] == YES)
        || ([regextestct evaluateWithObject:mobileNum] == YES)
        || ([regextestcu evaluateWithObject:mobileNum] == YES))
    {
        return YES;
    }
    else
    {
        return NO;
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)dealloc{
    if ([self.timer isValid]) {
        [self.timer invalidate];
    }
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    
    [self.view endEditing:YES];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    
    [self.view endEditing:YES];
    return YES;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
